<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>

<body>
<div class="layuimini-container" id="vue">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">团队名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="teamName" autocomplete="off" class="layui-input" id="teamName">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">

                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>

            </div>

        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">

            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="show">查看</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script type="text/html" id="stateTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="sex" id="{{d.id}}" value="{{d.approve}}" lay-skin="switch" lay-text="认证|封禁" lay-filter="state" {{ d.approve == true ? 'checked' : '' }}>
</script>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/vue.js"></script>
<script src="../../api/urlConfig.js"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>

<script>




    layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var upload = layui.upload;







        table.render({
            elem: '#currentTableId',
            url:ipURL+"/caption/team/page",
            headers:{token:localStorage.token},
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data.data,//解析数据列表

                };

            },

            response: {
                statusCode: 0//规定成功的状态码，默认：0
            },
            text:{none: '无数据'},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true,hide:true},
                {field: 'name', width:150, title: '团队名称'},
                {field: 'description', width: 150, title: '团队介绍'},
                {field: 'count', width: 100, title: '团队人数'},
                {field: 'userName', width: 100, title: '团队负责人'},
                {field: 'invitationCode', width: 150, title: '邀请码'},
                {field: 'fileSizeCapacityShow', width: 150, title: '文件容量使用情况:MB'},
                {field: 'gmtCreated', width: 200, title: '创建时间'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            var teamName = $('input[name="teamName"]').val()
            var teamCaption = $('input[name="teamCaption"]').val()
            //执行搜索重载
            table.reload('currentTableId', {

                page: {
                    curr: 1
                }
                , where: {
                    name:teamName,
                }
            });

            return false;
        });




        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {

            if (obj.event === 'add') {  // 监听添加操作
                $('#name').attr("readonly",false)
                // 清空表单
                form.val("stuform", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": '' // "name": "value"
                    ,"name": ''
                    ,"description": ''

                });
                layer.open({
                    type: 1,
                    title: '添加团队',
                    area: ['350px', '300px'],

                    content: $('#save'),
                    btn: ['提交', '退出'],
                    end:function(){
                        $('#save').css('display','none')
                    },
                    yes:function(){
                    },

                    success:function(layero,index){

                        layui.use('form',function(){
                            var form=layui.form;
                            layero.addClass('layui-form');
                            var submitBtn=layero.find('.layui-layer-btn0');
                            submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
                            layero.keydown(function(e){
                                if(e.keyCode==13){
                                    submitBtn.click();
                                }
                            });

                            form.on('submit(formVerify)',function(data){

                                $.ajax({
                                    url: ipURL + '/caption/team',
                                    method: 'post',
                                    data: JSON.stringify(data.field),
                                    dataType: 'JSON',
                                    contentType: 'application/json;charset=UTF-8',
                                    beforeSend: function (request) {
                                        request.setRequestHeader("token", localStorage.token);
                                    },
                                    success: function (res) {
                                        layer.msg(res.message);
                                        if (res.state){
                                            layer.close(index);
                                            layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID
                                        }




                                    },
                                    error: function (res) {
                                        layer.msg(res.message);
                                    }
                                });


                                return false;
                            });
                        });
                    }
                })
            }  if (obj.event === 'delete') {  // 监听删除操作

                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var ids = "";
                for(i=0;i<data.length;i++){
                    ids+=data[i].id;
                    ids+=",";
                }

                $.ajax({
                    url: ipURL + '/caption/team/'+ids,
                    method: 'delete',
                    beforeSend: function (request) {
                        request.setRequestHeader("token", localStorage.token);
                    },
                    success: function (res) {
                        layer.msg(res.message);
                        table.reload('currentTableId'); //刷新表格，参数为表格ID
                    },
                    error: function (data) {
                        layer.msg(res.message);
                    }
                });

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {

        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                $('#name').attr("readonly",true)
                //给表单赋值
                form.val("stuform", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": data.id // "name": "value"
                    ,"name": data.name
                    ,"description": data.description
                });
                layer.open({
                    type: 1,
                    title: '修改团队',
                    area: ['350px', '300px'],

                    content: $('#save'),
                    btn: ['提交', '退出'],
                    end:function(){
                        $('#save').css('display','none')
                    },
                    yes:function(){
                    },

                    success:function(layero,index){

                        layui.use('form',function(){
                            var form=layui.form;
                            layero.addClass('layui-form');
                            var submitBtn=layero.find('.layui-layer-btn0');
                            submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
                            layero.keydown(function(e){
                                if(e.keyCode==13){
                                    submitBtn.click();
                                }
                            });

                            form.on('submit(formVerify)',function(data){

                                $.ajax({
                                    url: ipURL + '/caption/team',
                                    method: 'put',
                                    data: JSON.stringify(data.field),
                                    dataType: 'JSON',
                                    contentType: 'application/json;charset=UTF-8',
                                    beforeSend: function (request) {
                                        request.setRequestHeader("token", localStorage.token);
                                    },
                                    success: function (res) {
                                        layer.msg(res.message);
                                        if (res.state){
                                            layer.close(index);
                                            layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID
                                        }

                                    },
                                    error: function (res) {
                                        layer.msg(res.message);
                                    }
                                });


                                return false;
                            });
                        });
                    }
                })
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('此项操作会删除团队下的所有任务以及记录,请三思', function (index) {
                    obj.del();
                    $.ajax({
                        url: ipURL + '/caption/team/'+data.id,
                        method: 'delete',
                        beforeSend: function (request) {
                            request.setRequestHeader("token", localStorage.token);
                        },
                        success: function (res) {
                            layer.msg(res.message);

                        },
                        error: function (res) {
                            layer.msg(res.message);
                        }
                    });
                })
            }else if (obj.event === 'show') {

                var index = layer.open({
                    title: data.name + "团队信息",
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'team-user.html',
                    success: function (layero, index) {
                        var iframe = window['layui-layer-iframe' + index];

                        iframe.child(data);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

    });
</script>

</body>




<form class="layui-form myData" action="save" method="post" lay-filter="stuform" id="save" style="display: none">
    <input type="hidden" name="id"  />

    <div class="layui-form-item">
        <label class="layui-form-label">团队名称：</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required"  class="layui-input" id="name"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">团队描述：</label>
        <div class="layui-input-block">
            <input type="text" name="description" lay-verify="required"  class="layui-input" />
        </div>
    </div>
</form>

</html>